package com.zhupanlin.order.controller;

import com.zhupanlin.order.bean.Order;
import com.zhupanlin.order.properties.OrderProperties;
import com.zhupanlin.order.service.OrderService;
import com.zhupanlin.product.bean.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 描述信息：
 *
 * @author zhupanlin
 * @version 1.0
 * @date 2025/1/23 15:10
 */
// @RefreshScope // 激活自动刷新功能
@RestController
// @RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    
    // @Value("${order.timeout}")
    // String orderTimeout;
    // @Value("${order.auto-confirm}")
    // String orderAutoConfirm;
    
    @Autowired
    private OrderProperties orderProperties;
    
    @GetMapping("config")
    public String config() {
        return "order.timeout = " + orderProperties.getTimeout() + 
                "\t order.auto-confirm = " + orderProperties.getAutoConfirm() + 
                "\t order.db-url = " + orderProperties.getDbUrl();
    }

    @GetMapping("/create")
    public Order createOrder(@RequestParam("productId") Long productId,
                             @RequestParam("userId") Long userId) {
        Order order = orderService.createOrder(productId, userId);
        return order;

    }

    @GetMapping("/seckill")
    public Order seckill(@RequestParam("productId") Long productId,
                             @RequestParam("userId") Long userId) {
        Order order = orderService.createOrder(productId, userId);
        order.setId(Long.MAX_VALUE);
        return order;

    }
    
    @GetMapping("/writeDb")
    public String writeDb() {
        return "writeDb success";
    }

    @GetMapping("/readDb")
    public String readDb() {
        return "readDb success";
    }


}